package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.AlbumAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AlbumUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.service.IAlbumService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/albums")
@Api(tags = "01. 相册管理模块")
@Validated
@Slf4j
public class AlbumController {
    @Autowired
    private IAlbumService albumService;

    // http://localhost:9999/album/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加相册")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid AlbumAddNewDTO albumAddNewDTO) {
        log.debug("开始处理【添加相册】的请求,参数: {}", albumAddNewDTO);
        albumService.addNew(albumAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9999/albums/9527/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除相册")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "请提交有效的ID值!") Long id) {
        log.debug("开始处理【根据ID删除相册】的请求，参数：{}", id);
        albumService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9999/albums/9527/update
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改相册")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfo(@PathVariable @Range(min = 1, message = "请提交有效的ID值!") Long id,
                                       @Valid AlbumUpdateDTO albumUpdateDTO) {
        log.debug("开始处理【修改相册】的请求，ID：{}，新数据：{}", id, albumUpdateDTO);
        albumService.updateInfo(id, albumUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9999/albums/9527
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询相册详情")
    @ApiOperationSupport(order = 410)
    public JsonResult<AlbumStandardVO> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【根据ID查询相册详情】的请求，参数：{}", id);
        AlbumStandardVO queryResult = albumService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    // http://localhost:9999/albums
    @GetMapping("")
    @ApiOperation("查询相册列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<AlbumListItemVO>> list() {
        log.debug("开始处理【相册列表】的请求,无参数");
        List<AlbumListItemVO> list = albumService.list();
        return JsonResult.ok(list);
    }

    // http://localhost:9999/albums/rebuild-cache
    @PostMapping("/rebuild-cache")
    @ApiOperation("重建相册缓存")
    @ApiOperationSupport(order = 900)
    public JsonResult<Void> rebuildCache(){
        log.debug("开始处理【重建相册缓存】的请求, 无参数");
        albumService.rebuildCache();
        return JsonResult.ok();
    }

}
